﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Badger.Vhdl.Types;
using Weazel.Badger.Vhdl.Expressions;
using Weazel.Badger.Vhdl.Statements;

#endregion

namespace Weazel.Badger.Vhdl
{
  public class ConversionBuilder
  {
    /// <summary>
    /// Applies the conversions given in the 'conversions' argument
    /// to the the expression 'e'
    /// </summary>
    /// <param name="conversions">conversions to apply</param>
    /// <param name="e">expression to apply conversion to</param>
    /// <returns>expression with applied conversions</returns>
    public static Expression ApplyConversion(
			TypeConversionResult conversions, 
			StatementCollection statements, 
			Entity entity,
			Process process,
			Expression e)
    {
      if (conversions == null)
        throw new NullReferenceException("List of conversions not set");

      // a depth of 1 means 'no conversions' necessary
      if (conversions.Depth == 1)
        return e;
			      
      for (int i = 1; i < conversions.Conversions.Length; i++)
      {
        Types.Type from = conversions.Conversions[i - 1];
        Types.Type to = conversions.Conversions[i];

				e = new ConversionExpression(statements, e, from, to);				              
      }

      return e;      
    }    
  }
}
